/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class ContTemDetail implements Serializable{
    private String id;
    private ContentTemplate contentTemId;
    private String propertyName;
    private String Status;

    public ContTemDetail() {
    }    

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ContentTemplate getContentTemId() {
        return contentTemId;
    }

    public void setContentTemId(ContentTemplate contentTemId) {
        this.contentTemId = contentTemId;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String Status) {
        this.Status = Status;
    }
    
    public static List<ContTemDetail> allCTD(String id) throws Exception{
        List<ContTemDetail> list = new ArrayList<ContTemDetail>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select * from md_content_template_details where content_template_id like '%"+id+"%' ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                ContTemDetail c = new ContTemDetail();
                c.setId(rs.getString(1));
                ContentTemplate ct = new ContentTemplate();
                ct.setId(rs.getString(2));
                c.setContentTemId(ct);
                c.setPropertyName(rs.getString(3));
                c.setStatus(rs.getString(4));
                list.add(c);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }    
    public static List<ContTemDetail> allCTDNoLike(String id) throws Exception{
        List<ContTemDetail> list = new ArrayList<ContTemDetail>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select * from md_content_template_details where content_template_id = "+id+" ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                ContTemDetail c = new ContTemDetail();
                c.setId(rs.getString(1));
                ContentTemplate ct = new ContentTemplate();
                ct.setId(rs.getString(2));
                c.setContentTemId(ct);
                c.setPropertyName(rs.getString(3));
                c.setStatus(rs.getString(4));
                list.add(c);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }    
    public static List<ContTemDetail> getPropertyByIdItem(String id) throws Exception{
        List<ContTemDetail> list = new ArrayList<ContTemDetail>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select * from md_item_properties "
                + " where item_id = "+id+" ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                ContTemDetail c = new ContTemDetail();                
                c.setPropertyName(rs.getString(3));
                c.setId(rs.getString(4));
                list.add(c);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }    
    
    public boolean insert() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;        
        try {
            cn = AppServer.getConnection();
            setId(Database.getSequenceValue(cn, "seq_md_content_template_detail"));
            String strSQL = " insert into md_content_template_details(detail_id,content_template_id,property_name,status) "
                    + "values(?,?,?,?) ";
            pst = cn.prepareStatement(strSQL);
            
            pst.setInt(1, Integer.parseInt(getId()));            
            pst.setInt(2, Integer.parseInt(getContentTemId().getId()));
            pst.setString(3, getPropertyName());
            pst.setInt(4, Integer.parseInt(getStatus()));
            
            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    public boolean update() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        String strSQL = " update md_content_template_details "
                + " set property_name = ? , status = ? "
                + " where detail_id = ? ";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            
            pst.setString(1, getPropertyName());
            pst.setInt(2, Integer.parseInt(getStatus()));
            pst.setInt(3, Integer.parseInt(getId()));
            
            return pst.executeUpdate() > 0;            
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }

    public boolean remove() throws Exception{
        Connection cn = null;
        Statement st = null;

        if (id != null || !id.equalsIgnoreCase("")) {
            String strSQL = " delete from md_content_template_details where detail_id = "+getId()+" ";
            try {
                cn = AppServer.getConnection();
                st = cn.createStatement();
                return st.executeUpdate(strSQL) > 0;
            } finally {
                Database.closeObject(st);
                Database.closeObject(cn);
            }
        }
        return true;
    }
}
